package com.newcapec.erp.utils;

import com.newcapec.erp.bean.*;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

public class XMLFWriter {
    public static void ChangeAdmin(AdminEntity adminEntity) {
        Document document = getDocument("admin.xml");
        List<Element> list = document.selectNodes("/data/admin");
        Element e=list.get(0);
        e.element("adminAn").setText(adminEntity.getAdminAn());
        e.element("adminName").setText(adminEntity.getAdminName());
        e.element("adminTel").setText(adminEntity.getAdminTel());
        e.element("adminMail").setText(adminEntity.getAdminMail());
        XMLSave("admin.xml",document);
    }

    public static void XMLSave(String fileName, Document doc){
        OutputFormat of = OutputFormat.createPrettyPrint();
        of.setEncoding("utf-8");
        try {
            org.dom4j.io.XMLWriter xmlWriter=new org.dom4j.io.XMLWriter(new FileWriter("src/"+fileName),of);
            xmlWriter.write(doc);
            xmlWriter.close();
        } catch (IOException e) {
            System.out.println(fileName+"文件读取失败!");
        }

    }

    //获取指定文件对应的document对象 (公用方法)
    private static Document getDocument(String fileName) {
        //1.用SAXReader类，创建一个解析器
        SAXReader sr = new SAXReader();
        //2.获取需要被解析的xml文档
        Document document = null;
        try {
            //3.用解析器解析文档，生成dom4j文档对象
            document = sr.read(new File("src/" + fileName));
        } catch (DocumentException e) {
            System.out.println("解析xml异常");
        }
        return document;
    }

    public static void addWarehouse(WarehouseEntity warehouseEntity) {
        Document document=getDocument("warehouses.xml");
        List<Element> list = document.selectNodes("/warehouses");
        Element element1 = list.get(0);
        Element element = element1.addElement("warehouse");
        element.addElement("code").addText(warehouseEntity.getCode());
        element.addElement("name").addText(warehouseEntity.getName());
        element.addElement("attr").addText(warehouseEntity.getAttr());
        // 保存操作
        XMLSave("warehouses.xml",document);
    }

    public static void addStore(StoreEntity storeEntity) {
        Document document=getDocument("stores.xml");
        List<Element> list = document.selectNodes("/stores");
        Element element1 = list.get(0);
        Element element = element1.addElement("store");
        element.addElement("basedWarehouse").addText(storeEntity.getBasedWarehouse());
        element.addElement("basedCustomer").addText(storeEntity.getBasedCustomer());
        element.addElement("storeType").addText(storeEntity.getStoreType());
        element.addElement("storeAttr").addText(storeEntity.getStoreAttr());
        element.addElement("productAttr").addText(storeEntity.getProductAttr());
        element.addElement("status").addText(storeEntity.getStatus());
        // 保存操作
        XMLSave("stores.xml",document);
    }

    //添加客户
    public static void addClient(ClientEntity clientEntity) {
        Document document=getDocument("client.xml");
        List<Element> list = document.selectNodes("/clientEntitys");
        Element element1 = list.get(0);
        Element element = element1.addElement("clientEntity");
        element.addElement("chinese").addText(clientEntity.getChinese());
        element.addElement("CN").addText(clientEntity.getCN());
        element.addElement("QYSX").addText(clientEntity.getQYSX());
        element.addElement("site").addText(clientEntity.getSite());
        element.addElement("principal").addText(clientEntity.getPrincipal());
        element.addElement("phone").addText(clientEntity.getPhone());
        // 保存操作
        XMLSave("client.xml",document);
    }

    //添加库存
    public static void addInventoryEntity(InventoryEntity inventoryEntity) {
        Document document=getDocument("/inventory.xml");
        List<Element> list = document.selectNodes("/inventorys");
        Element element1 = list.get(0);
        Element element = element1.addElement("inventory");
        element.addElement("stockType").addText(inventoryEntity.getStockType());
        element.addElement("commodityCode").addText(inventoryEntity.getCommodityCode());
        element.addElement("productName").addText(inventoryEntity.getProductName());
        element.addElement("PCS").addText(inventoryEntity.getPCS());
        element.addElement("customerName").addText(inventoryEntity.getCustomerName());
        element.addElement("expiringDate").addText(inventoryEntity.getExpiringDate());
        // 保存操作
        XMLSave("inventory.xml",document);
    }

    public static void addSystem(UserEntity systemEntity) {
        Document document=getDocument("system.xml");
        List<Element> list = document.selectNodes("/data/users");
        Element element1 = list.get(0);
        Element element = element1.addElement("user");
        element.addElement("userAn").addText(systemEntity.getUserAn());
        element.addElement("userName").addText(systemEntity.getUserName());
        element.addElement("userOrganize").addText(systemEntity.getUserOrganize());
        element.addElement("userRole").addText(systemEntity.getUserRole());
        element.addElement("CreateTime").addText(systemEntity.getCreateTime());
        element.addElement("userState").addText(systemEntity.getUserState());
        // 保存操作
        XMLSave("system.xml",document);
    }

    public static void deleteWarehouse(int index) {
        Document document=getDocument("warehouses.xml");
        List<Element> list = document.selectNodes("/warehouses/warehouse");
        list.get(index).getParent().remove(list.get(index));
        XMLSave("warehouses.xml",document);
    }

    public static void deleteStore(int index) {
        Document document=getDocument("stores.xml");
        List<Element> list = document.selectNodes("/stores/store");
        list.get(index).getParent().remove(list.get(index));
        XMLSave("stores.xml",document);
    }

    //删除客户
    public static void deleteClient(int index) {
        Document document=getDocument("client.xml");
        List<Element> list = document.selectNodes("/clientEntitys/clientEntity");
        list.get(index).getParent().remove(list.get(index));
        XMLSave("client.xml",document);
    }

    //删除库存
    public static void deleteInventory(int index) {
        Document document=getDocument("inventory.xml");
        List<Element> list = document.selectNodes("/inventorys/inventory");
        list.get(index).getParent().remove(list.get(index));
        XMLSave("inventory.xml",document);
    }

    public static void deleteSystem(int index) {
        Document document=getDocument("system.xml");
        List<Element> list = document.selectNodes("/data/users/user");
        list.get(index).getParent().remove(list.get(index));
        XMLSave("system.xml",document);
    }

    //修改仓库信息
    public static void UpdateWarehouse(int index, WarehouseEntity newWarehouseEntity) {
        Document document=getDocument("warehouses.xml");
        List<Element> list = document.selectNodes("/warehouses/warehouse");
        list.get(index).element("code").setText(newWarehouseEntity.getCode());
        list.get(index).element("name").setText(newWarehouseEntity.getName());
        list.get(index).element("attr").setText(newWarehouseEntity.getAttr());
        XMLSave("warehouses.xml",document);
    }

    //修改储位信息
    public static void UpdateStore(int index, StoreEntity newStoreEntity) {
        Document document=getDocument("stores.xml");
        List<Element> list = document.selectNodes("/stores/store");
        list.get(index).element("basedWarehouse").setText(newStoreEntity.getBasedWarehouse());
        list.get(index).element("basedCustomer").setText(newStoreEntity.getBasedCustomer());
        list.get(index).element("storeType").setText(newStoreEntity.getStoreType());
        list.get(index).element("storeAttr").setText(newStoreEntity.getStoreAttr());
        list.get(index).element("productAttr").setText(newStoreEntity.getProductAttr());
        list.get(index).element("status").setText(newStoreEntity.getStatus());
        XMLSave("stores.xml",document);
    }



    //修改系统信息
    public static void UpdateSystem(int index, UserEntity newSystemEntity) {
        Document document=getDocument("system.xml");
        List<Element> list = document.selectNodes("/data/users/user");
        list.get(index).element("userAn").setText(newSystemEntity.getUserAn());
        list.get(index).element("userName").setText(newSystemEntity.getUserName());
        list.get(index).element("userOrganize").setText(newSystemEntity.getUserOrganize());
        list.get(index).element("userRole").setText(newSystemEntity.getUserRole());
        list.get(index).element("CreateTime").setText(newSystemEntity.getCreateTime());
        list.get(index).element("userState").setText(newSystemEntity.getUserState());
        XMLSave("system.xml",document);
    }


    //修改客户信息
    public static void UpdateClient(int index, ClientEntity newClientEntity) {
        Document document=getDocument("client.xml");
        List<Element> list = document.selectNodes("/clientEntitys/clientEntity");
        list.get(index).element("chinese").setText(newClientEntity.getChinese());
        list.get(index).element("CN").setText(newClientEntity.getCN());
        list.get(index).element("QYSX").setText(newClientEntity.getQYSX());
        list.get(index).element("site").setText(newClientEntity.getSite());
        list.get(index).element("principal").setText(newClientEntity.getPrincipal());
        list.get(index).element("phone").setText(newClientEntity.getPhone());

        XMLSave("client.xml",document);
    }


    //修改库存信息
    public static void UpdateInventoryEntity(int index, InventoryEntity newInventoryEntity) {
        Document document=getDocument("inventory.xml");
        List<Element> list = document.selectNodes("/inventorys/inventory");
        list.get(index).element("stockType").setText(newInventoryEntity.getStockType());
        list.get(index).element("commodityCode").setText(newInventoryEntity.getCommodityCode());
        list.get(index).element("productName").setText(newInventoryEntity.getProductName());
        list.get(index).element("PCS").setText(newInventoryEntity.getPCS());
        list.get(index).element("customerName").setText(newInventoryEntity.getCustomerName());
        list.get(index).element("expiringDate").setText(newInventoryEntity.getExpiringDate());

        XMLSave("inventory.xml",document);
    }
}


